package com.pubinfo.passbook.common.service.contract.impl;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mashape.unirest.http.exceptions.UnirestException;
import com.pubinfo.passbook.common.entity.TContractWriteoffHeader;
import com.pubinfo.passbook.common.entity.TPassbook;
import com.pubinfo.passbook.common.entity.TSourcePassbookReimburse;
import com.pubinfo.passbook.common.entity.TWiteoffInstance;
import com.pubinfo.passbook.common.mapper.TContractWriteoffHeaderMapper;
import com.pubinfo.passbook.common.mapper.TContractWriteoffItemsMapper;
import com.pubinfo.passbook.common.mapper.TContractWriteoffRelationMapper;
import com.pubinfo.passbook.common.model.dto.contract.ContractIdParam;
import com.pubinfo.passbook.common.model.dto.contract.WriteOffBackParam;
import com.pubinfo.passbook.common.model.http.request.GainWriteoffInstStatus.GainWriteoffInstStatusItems;
import com.pubinfo.passbook.common.model.http.response.GetWriteoffInstStatus.WriteoffInstStatusItem;
import com.pubinfo.passbook.common.model.http.response.GetWriteoffInstStatus.WriteoffInstStatusMessage;
import com.pubinfo.passbook.common.model.vo.base.JsonResult;
import com.pubinfo.passbook.common.model.vo.contract.ContractWriteOffVo;
import com.pubinfo.passbook.common.model.vo.contract.WriteOffRollbackVo;
import com.pubinfo.passbook.common.service.contract.TContractWriteoffHeaderService;
import com.pubinfo.passbook.common.service.contract.TContractWriteoffRelationService;
import com.pubinfo.passbook.common.service.dedicated.DedicatedService;
import com.pubinfo.passbook.common.service.mss.HttpService;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.format.FormatUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 全
 * @description 针对表【t_contract_writeoff_header(报账单预制表-基本信息)】的数据库操作Service实现
 * @createDate 2021-12-07 15:13:39
 */
@Service
@Slf4j
public class TContractWriteoffHeaderServiceImpl extends ServiceImpl<TContractWriteoffHeaderMapper, TContractWriteoffHeader> implements TContractWriteoffHeaderService {

    @Resource
    private DedicatedService dedicatedService;

    @Resource
    private TContractWriteoffRelationService writeoffRelationService;

    /**
     * 获取税金计提明细
     */
    @Override
    public List<ContractWriteOffVo> getContractWriteOffList(Integer contractId) {
        return this.baseMapper.getContractWriteOffList(contractId);
    }

    @Override
    public void getWriteoffInstStatus() throws IOException, UnirestException {
        List<TContractWriteoffHeader> tWiteoffInstances = this.baseMapper.selectStatusNotReady();
        for (TContractWriteoffHeader tWiteoffInstance : tWiteoffInstances) {
            List<GainWriteoffInstStatusItems> list = new ArrayList<>();
            list.add(new GainWriteoffInstStatusItems(tWiteoffInstance.getOtherSystemMainId()));
            //获取报账单状态
            WriteoffInstStatusMessage writeoffInstStatusMessage = dedicatedService.getWriteoffInstStatusBodyItem(list);
            if (!CollectionUtils.isEmpty(writeoffInstStatusMessage.getItems())) {
                WriteoffInstStatusItem instStatus = writeoffInstStatusMessage.getItems().get(0);
                UpdateWrapper<TContractWriteoffHeader> updateWrapper = new UpdateWrapper<>();
                    updateWrapper.eq("writeoff_id", tWiteoffInstance.getWriteoffId());
                    updateWrapper.set("witeoff_Instance_code", instStatus.getWriteoffInstanceCode());//被共享附件的报账单单号
                    updateWrapper.set("status", instStatus.getStatus());//状态
                    updateWrapper.set("sap_certificate_code", instStatus.getSapCertificateCode());//sap凭证号,状态已生成SAP凭证时为必须
                    updateWrapper.set("year", instStatus.getYear());//年度
                    updateWrapper.set("sap_creator", instStatus.getSapCreator());//制证人账号，状态已生成SAP凭证时为必须，可在清账时作为清账人反馈
                    //updateWrapper.set("account_code", instStatus.getAccountCode());//科目编码供应商
                    updateWrapper.set("abstract_", instStatus.getAbstract_());//说明，-3状态对应的发起失败信息
                    updateWrapper.set("audit_sum", instStatus.getAuditSum());//核定金额
                    updateWrapper.set("act_name", instStatus.getActName());//当前处理环节
                    updateWrapper.set("handler_account", instStatus.getHandlerAccount());//当前处理人账号
                    baseMapper.update(null, updateWrapper);
                    log.info(tWiteoffInstance.getWriteoffId() + "状态已更新");
                    //-4-等待生成报账单中;-3-报账单发起失败；-1-报账单删除，－2-退单；2-生成报账单成功；3-已生成SAP凭证
                    if (instStatus.getStatus().equals("-3") || instStatus.getStatus().equals("-2") || instStatus.getStatus().equals("-1")){
                        //TODO 删除挑对记录，保留报账单
                        writeoffRelationService.getBaseMapper().deleteById(tWiteoffInstance.getWriteoffId());
                    }
                }


            }
        }

    @Override
    public IPage<WriteOffRollbackVo> getReimbursemenList(WriteOffBackParam param) {
        IPage<WriteOffRollbackVo> ipage=page(param.getPage());
       return baseMapper.getReimbursemenList(ipage,param);
    }

}



